﻿using HostAssistantBLL;
using HostAssistantCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HostAssistant
{
    public partial class rollbaksite : U1Form
    {
        string  SitesName = null;
        string name = "";
        string dbname = "";
        string bakname = "";
        public rollbaksite(string _sf)
        {
            SitesName = _sf;
            InitializeComponent();
        }
        private void addsites_Load(object sender, EventArgs e)
        {
            var slist = sitebll.GetsiteList();
            var sf = slist.FirstOrDefault(o => o.name == SitesName);
            lblname.Text = sf?.name ?? "";
            String path = PathSetbll.GetPathSet()?.SiteBakPath ?? "";
            bakname = path + "\\"+lblname.Text+"\\";
            var files = Directory.GetFiles(bakname , "*.zip");
          
            if(files!=null && files.Length>0)
            {
                foreach (var file in files)
                {
                    comboBoxEdit1.Properties.Items.Add(Path.GetFileName(file));

                }
            }
         
        }

        #region 返回
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 还原站点
        private void simpleButton5_Click(object sender, EventArgs e)
        {

            try
            {


                OpHelper.CallWithTimeout(Addmssql, 300000);


            }
            catch (Exception ex)
            {
                NLogger.Error(ex.ToString());
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }




        }


        void Addmssql()
        {
            splashScreenManager1.ShowWaitForm();
            splashScreenManager1.SetWaitFormCaption("请等待");
            splashScreenManager1.SetWaitFormDescription("加载中...");
            if (comboBoxEdit1.Text == "选择文件")
            {
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择要恢复的备份文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            var set = Data<HostAssistantCore.PathSet>.GetInstance().GetModelList("select *  from PathSet", null).FirstOrDefault();
            string _depositPath = set.SiteBakPath+ "\\" + lblname.Text + "\\" + comboBoxEdit1.Text;
            var slist = sitebll.GetsiteList();
            var sf = slist.FirstOrDefault(o => o.name == lblname.Text);
            string _floderPath = sf.path+"\\temp\\";
                if (!Directory.Exists(_floderPath))
                {
                    Directory.CreateDirectory(_floderPath);
                }
                if (!File.Exists(_depositPath))
                {
                splashScreenManager1.CloseWaitForm();
                DevExpress.XtraEditors.XtraMessageBox.Show("压缩文件不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
             
                 ZipClass.UnZip(_depositPath, _floderPath, "");

                sopDirectory(_floderPath, sf.path);



            splashScreenManager1.CloseWaitForm();
            DevExpress.XtraEditors.XtraMessageBox.Show("还原站点成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Close();
            return;
        }



     
      
        bool sopDirectory(string sourcePath,string destPath)
        {
            try
            {
                //string sourcePath = System.Windows.Forms.Application.StartupPath + "/temp/j/";
                string ProgramName = Path.GetFileName(Application.ExecutablePath);
               // string destPath = System.Windows.Forms.Application.StartupPath.Replace(ProgramName.Replace(".exe", "").Replace("App工具", ""), "");
                string floderName = Path.GetFileName(sourcePath);
                DirectoryInfo di = Directory.CreateDirectory(Path.Combine(destPath, floderName));
                string[] files = Directory.GetFileSystemEntries(sourcePath);
                NLogger.Info("覆盖:" + string.Join(",", files));
                foreach (string file in files)
                {


                    
                    NLogger.Info("覆盖:" + file);

                    if (Directory.Exists(file))
                    {
                        opDirectory(file, di.FullName);
                    }
                    else
                    {
                        File.Copy(file, Path.Combine(di.FullName, Path.GetFileName(file)), true);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
                NLogger.Error("---覆盖失败：" + ex.ToString());
                return false;
            }
        }

        void opDirectory(string sourcePath, string destPath)
        {
            string floderName = Path.GetFileName(sourcePath);
            DirectoryInfo di = Directory.CreateDirectory(Path.Combine(destPath, floderName));
            string[] files = Directory.GetFileSystemEntries(sourcePath);

            foreach (string file in files)
            {
                if (Directory.Exists(file))
                {
                    opDirectory(file, di.FullName);
                }
                else
                {
                    File.Copy(file, Path.Combine(di.FullName, Path.GetFileName(file)), true);
                }
            }
        }
    
        #endregion

    }
}